#include "ffmpegutils.h"
#include <QFile>
#include <QDebug>
FFMPEGUtils::FFMPEGUtils()
{

}

void FFMPEGUtils::pcmTowav(WAVHeadStructure &wavHeadStructure, const char *pcmFilePath, const char *wavFilePath)
{

    QFile pcmFile(pcmFilePath);

    if(!pcmFile.open(QFile::ReadOnly)) {

        qDebug() << "pcm file open fail";
        return;
    }

    wavHeadStructure.byteRate = (wavHeadStructure.numChannels * wavHeadStructure.bitPerSample) >> 3;
    wavHeadStructure.blockAlign = wavHeadStructure.sampleRate * wavHeadStructure.byteRate;

    int pcmSize = pcmFile.size();
    int allSize = sizeof(wavHeadStructure) + pcmSize;

    wavHeadStructure.chunkAllSize = allSize - 8;
    wavHeadStructure.dataChunkSize = pcmSize;

    QFile wavFile(wavFilePath);


    if(!wavFile.open(QFile::WriteOnly)) {

        qDebug() << "wav file open fail";
        pcmFile.close();
        return;
    }

    wavFile.write((char *)&wavHeadStructure,sizeof(wavHeadStructure));

    char buff[1024];

    while (true) {

        int len = pcmFile.read(buff,sizeof(buff));

        if (len <= 0) {
            break;
        }

        wavFile.write(buff,len);
    }


    wavFile.close();
    pcmFile.close();
}

